<?php


    //$wca = new Authentication($_GET["timestamp"],$_GET["nonce"]);

    //$authString = $wca->getAuthString();

    //$data[] = array($this->getToken(),$this->getTimeStamp(),$this->getNonce());
    $appID = 'wx67c44b4e928453d5';
    $appSecret = '6ee1a1a3419a305b96c8c52740fed1e6';

    $data[] = "test123";
    $data[] = $_GET["timestamp"];
    $data[] = $_GET["nonce"];

    asort($data);

    $d ='';
    $authString = '';

    foreach($data as $d){
        $authString .= $d;
    }

    //this section is to verify from wechat
    if(sha1($authString) == $_GET['signature']){

        if(!empty($_GET['echostr'])){
            echo $_GET['echostr'];
            die();

        }else{
            //capturing everything directly posted to script
            $body = file_get_contents('php://input');

            //database properties
            $dbhost = 'localhost:3036';
            $dbuser = 'movie_user_dummy';
            $dbpass = '$kjdf@#42alp';
            $conn = mysql_connect($dbhost, $dbuser, $dbpass);//returns MySQL link identifier on success or FALSE on failure.

            //checks if connection is successful
            if(! $conn )
                die('Could not connect: ' . mysql_error());

            //create
            $sql_insert = "INSERT INTO wechatdata".
                "(id,xml_in,date_in,get_params)".
                "VALUES ".
                "('','.$body.','','')";

            mysql_select_db( 'cinemo_dummy' );//returns TRUE on success or FALSE on failure.

            $retval = mysql_query( $sql_insert, $conn );

            if(! $retval )
                die('Could not enter data: ' . mysql_error());

            mysql_close($conn);


            //extract xml from we-chat response message
            $content = simplexml_load_string($body);

            echo postRichMediaMessage($content);


        }

    }else{

    }

    /*
     *generate generic reply message
     *@param string $content
     *@return string xml
     *
     * */
    function postReplyMessage($content){


        $textTpl = '<xml>
                <ToUserName><![CDATA[%s]]></ToUserName>
                <FromUserName><![CDATA[%s]]></FromUserName>
                <CreateTime>%s</CreateTime>
                <MsgType><![CDATA[%s]]></MsgType>
                <Content><![CDATA[%s]]></Content>
                <FuncFlag>0</FuncFlag>
            </xml>';

        $text = sprintf($textTpl, $content->FromUserName,$content->ToUserName, time(), "text", "Hello back at you");
        return $text;
    }

function postRichMediaMessage($content){
      $textTpl = '<xml>
                 <ToUserName><![CDATA[%s]]></ToUserName>
                 <FromUserName><![CDATA[%s]]></FromUserName>
                 <CreateTime>%s</CreateTime>
                 <MsgType><![CDATA[%s]]></MsgType>
                 <ArticleCount>1</ArticleCount>
                 <Articles>
                 <item>
                    <Title><![CDATA[Welcome Test]]></Title>
                    <PicUrl><![CDATA[%s]]></PicUrl>
                     <Url><![CDATA[%s]]></Url>
                 </item>
                 </Articles>
                 </xml> ';

    $text = sprintf($textTpl, $content->FromUserName,$content->ToUserName, time(), "news", "http://162.13.182.84/cinemowechat/one2.jpg","http://162.13.182.84/cinemowechat/RedirectTest.php");
    return $text;
}

